package one.stand.controller;

import com.google.common.collect.Lists;
import com.jmp.api.crm.dto.*;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.feign.GoodsTypeFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import one.stand.mapper.GoodsApiCrmPcMapper;
import one.stand.model.ResultModel;
import one.stand.service.GoodsTypeService;
import one.stand.util.OneListUtil;
import one.stand.util.RequestContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api("产品分类管理")
@RestController
@RequestMapping("crmPc/goods/type")
public class GoodsTypeController {
    @Autowired
    private GoodsTypeFeign goodsTypeFeign;

    @Autowired
    private GoodsApiCrmPcMapper goodsApiCrmPcMapper;
    @Autowired
    private GoodsTypeService goodsTypeService;

    @ApiOperation(value = "产品分类列表")
    @PostMapping("/list")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel<GoodsTypeListResponse> list(@RequestAttribute GoodsTypeListRequest request) {
        final Integer userCompanyId = RequestContextHolder.getRequestContext().getUser().getCompanyId();

        List<GoodsTypeListResponse.Type> list = goodsApiCrmPcMapper.selectGoodsType(userCompanyId, null);

        List<GoodsTypeListResponse.Type> pageInfo = OneListUtil.group(list,
                type -> type.getParentId() == 0,
                GoodsTypeListResponse.Type::getGoodstypeId,
                GoodsTypeListResponse.Type::getParentId,
                (type, child) -> {
                    if (OneListUtil.isNotEmpty(type.getChildren())) {
                        type.getChildren().add(child);
                    } else {
                        type.setChildren(Lists.newArrayList(child));
                    }
                }
        );

        GoodsTypeListResponse response = GoodsTypeListResponse.builder()
                .types(pageInfo)
                .build();
        return ResultModel.success(response);
    }


    @ApiOperation(value = "添加/修改商品分类")
    @PostMapping("/add")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel<Integer> add(@RequestAttribute GoodsTypeSaveRequest request) {
        final Integer userCompanyId = RequestContextHolder.getRequestContext().getUser().getCompanyId();

        Integer goodsTypeId = goodsTypeService.save(userCompanyId, request.getName(), request.getParentId(), request.getGoodstypeId(), request.getIsShow(), null);

        return ResultModel.success(goodsTypeId);
    }

    @ApiOperation(value = "删除商品分类")
    @PostMapping("/delete")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel delete(@RequestAttribute GoodsTypeDelRequest request) {

        goodsTypeFeign.delete(com.jmp.base.dto.GoodsTypeDeleteRequest.builder().goodsTypeId(request.getGoodstypeId()).build());

        return ResultModel.success();
    }

    @ApiOperation(value = "上移/下移商品分类")
    @PostMapping("/move")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel move(@RequestAttribute GoodsTypeMoveRequest request) {
        final Integer userCompanyId = RequestContextHolder.getRequestContext().getUser().getCompanyId();

        goodsTypeService.updateSort(request.getGoodstypeId(), request.getType(), userCompanyId);

        return ResultModel.success();
    }
}
